﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
//using CommontLibrary;
using System.Data.SqlClient;
using Newtonsoft.Json;

namespace GetHistoryCall
{
    public partial class f100_get_ds_cuoc_goi : Form
    {
        public f100_get_ds_cuoc_goi()
        {
            InitializeComponent();
        }

        #region Members
        string m_str_json_string = "";
        string m_str_ds_so_dien_thoai = "";
        List<DALichSuCuocGoi> m_lst_lich_su_cuoc_goi = new List<DALichSuCuocGoi>();
        SqlConnection m_sql_conn = ConnectData.getConnection();
        #endregion

        #region Private Methods
        public static string chuan_hoa_so_dien_thoai(string ip_str_sdt)
        {

            try
            {
                string[] v_array = { ".", ",", " ", "-" };
                // Nếu có các ký tự đặc biệt, loại bỏ
                for (int v_i = 0; v_i < v_array.Length; v_i++)
                {
                    while (ip_str_sdt.Contains(v_array[v_i]))
                    {
                        ip_str_sdt = ip_str_sdt.Replace(v_array[v_i], "");
                    }
                }
                while (ip_str_sdt.Substring(0, 1) == "0")
                {
                    ip_str_sdt = ip_str_sdt.Substring(1, ip_str_sdt.Length - 1);
                }
                return ip_str_sdt.Substring(0, ip_str_sdt.Length - 6) + " " + ip_str_sdt.Substring(ip_str_sdt.Length - 6, 3) + " " + ip_str_sdt.Substring(ip_str_sdt.Length - 3, 3);
            }
            catch (Exception)
            {
                return "";
            }

        }
        private void get_json_string_history_call()
        {
            m_str_ds_so_dien_thoai = m_txt_ds_dien_thoai.Text;
            string strLinkClient = "http://203.162.121.70:88/TPCUtil/tpc/DoAction.jsp?event=GetListInfo&phoneList=" + m_str_ds_so_dien_thoai;
            System.Net.WebClient v_webclient = new System.Net.WebClient();
            m_str_json_string = v_webclient.DownloadString(strLinkClient);

            m_txt_json_string.Text = m_str_json_string;
        }

        private void insert_history_call_2_db(DALichSuCuocGoi ip_obj_lich_su_cuoc_goi)
        {
            if (ip_obj_lich_su_cuoc_goi.data.link_down_record != null &&
                ip_obj_lich_su_cuoc_goi.data.link_down_record.Trim() != "")
            {
                ip_obj_lich_su_cuoc_goi.connec = m_sql_conn;
                ip_obj_lich_su_cuoc_goi.data.mobile_phone = chuan_hoa_so_dien_thoai(ip_obj_lich_su_cuoc_goi.data.mobile_phone);
                ip_obj_lich_su_cuoc_goi.insert_miss_history_call(2);
            }
        }
        public static void WriteToLog(string ip_str_content_log)
        {
            string v_str_path;
            v_str_path = AppDomain.CurrentDomain.BaseDirectory + "Logs/";
            write_string_2_log_file(ip_str_content_log + Environment.NewLine, v_str_path);
        }
        private static void write_string_2_log_file(string i_str_error, string i_str_path)
        {
            System.IO.StreamWriter m_stream_writer;
            try
            {
                string v_str_filename;
                v_str_filename = i_str_path + DateTime.Now.ToString("yyyyMMddHHmm") + ".log";
                if (System.IO.File.Exists(v_str_filename))
                    m_stream_writer = System.IO.File.AppendText(v_str_filename);
                else m_stream_writer = System.IO.File.CreateText(v_str_filename);
                m_stream_writer.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss ") + i_str_error);
                m_stream_writer.Close();
            }
            finally
            {
            }
        }
        #endregion

        #region Events
        private void m_cmd_get_json_string_Click(object sender, EventArgs e)
        {
            try
            {
                get_json_string_history_call();
            }
            catch (Exception v_e)
            {
                throw v_e;
            }
        }

        private void m_cmd_insert_db_Click(object sender, EventArgs e)
        {
            try
            {
                DALichSuCuocGoi m_obj_lich_su_cuoc_goi = new DALichSuCuocGoi(m_sql_conn);
                m_str_json_string = m_txt_json_string.Text;
                m_lst_lich_su_cuoc_goi = JsonConvert.DeserializeObject<List<DALichSuCuocGoi>>(m_str_json_string);
                for (int v_i = 0; v_i < m_lst_lich_su_cuoc_goi.Count; v_i++)
                {
                    try
                    {
                         insert_history_call_2_db(m_lst_lich_su_cuoc_goi[v_i]);
                    }
                    catch (Exception)
                    {
                        WriteToLog(m_lst_lich_su_cuoc_goi[v_i].data.mobile_phone + " - " + m_lst_lich_su_cuoc_goi[v_i].data.link_down_record);
                    }
                    if (v_i == 580)
                    {
                        MessageBox.Show("OK");
                    }
                }
                MessageBox.Show("Lưu lịch sử cuộc gọi thành công!");
            }
            catch (Exception v_e)
            {
                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_str_json_string = m_txt_json_string.Text;
            m_txt_json_string.Text = "";
            string v_str_link = "";
            int v_i_count = 0;
            m_lst_lich_su_cuoc_goi = JsonConvert.DeserializeObject<List<DALichSuCuocGoi>>(m_str_json_string);
            for (int v_i = 0; v_i < m_lst_lich_su_cuoc_goi.Count; v_i++)
            {
                try
                {
                    if (m_lst_lich_su_cuoc_goi[v_i].data.link_down_record != null
                        && m_lst_lich_su_cuoc_goi[v_i].data.link_down_record != "")
                    {
                        v_str_link += "'" + m_lst_lich_su_cuoc_goi[v_i].data.link_down_record + "',";
                        v_i_count += 1;
                    }
                }
                catch (Exception)
                {
                    WriteToLog(m_lst_lich_su_cuoc_goi[v_i].data.mobile_phone + " - " + m_lst_lich_su_cuoc_goi[v_i].data.link_down_record);
                }
            }
            m_txt_json_string.Text = v_str_link;
            MessageBox.Show(v_i_count.ToString());
        }
        #endregion
    }
}
